
#include "cross_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"

namespace optiling
{
static ge::graphStatus TilingFunc(gert::TilingContext *context)
{

    CrossTilingData tiling;
    set_size_by_input(tiling, context, 0);
    CommonTiling<CrossTilingData> commonTiling;
    commonTiling.tiling = &tiling;
    commonTiling.context = context;
    commonTiling.data_size = tiling.get_size() / 3;
    commonTiling.vector_align_size = 32;
    commonTiling.input_arg_count = 2;
    commonTiling.output_arg_count = 1;
    commonTiling.mem_addition_num = 2;
    commonTiling.ub_scale = 3;
    commonTiling.apply();
    auto runtime_attrs = context->GetAttrs();
    auto dim = runtime_attrs->GetInt(0);
    auto shape = context->GetInputShape(0)->GetStorageShape();

    // 获取目标dim
    int64_t targetDim = *dim;
    auto shape_dim_num = shape.GetDimNum();
    if (targetDim >= shape_dim_num)
    {
        for (auto i = 0; i < shape.GetDimNum(); ++i)
        {
            if (shape.GetDim(i) == 3)
            {
                targetDim = i;
                break;
            }
        }
    }

    // 计算stride
    int64_t stride = 1;
    for (int64_t i = shape_dim_num - 1; i > targetDim; --i)
    {
        stride *= shape.GetDim(i);
    }

    tiling.set_stride(stride);

    int64_t preSize = 1;
    for (int64_t i = 0; i < targetDim; ++i)
    {
        preSize *= shape.GetDim(i);
    }

    tiling.set_preSize(preSize);
    tiling_zero_workspace(context);
    tiling_save_to_buffer(tiling, context);
    print_common_tiling(tiling);
    std::cout << "stride" << tiling.get_stride() << std::endl;
    std::cout << "preSize" << tiling.get_preSize() << std::endl;
    return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge
{
static ge::graphStatus InferShape(gert::InferShapeContext *context)
{
    const gert::Shape *x1_shape = context->GetInputShape(0);
    gert::Shape *y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops
{
class Cross : public OpDef
{
  public:
    explicit Cross(const char *name) : OpDef(name)
    {
        this->Input("x1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_INT8})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("dim").AttrType(OPTIONAL).Int(65530);

        this->SetInferShape(ge::InferShape);

        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");
    }
};

OP_ADD(Cross);
} // namespace ops
